package com.chushouya.common.util;

import com.general.framework.core.lang.Dates;
import com.general.framework.core.lang.IdTakes;
import com.general.framework.core.lang.Strings;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.*;

public class ProductUtil {
    public static String genProductCode() {
        final Date timeNow = Dates.getTimeNow();
        final String YEAR = Dates.format(timeNow, "yyyy").substring(3);
        final String DAYS = String.format("%03d", LocalDate.now().getDayOfYear());
        final String RANDOM = IdTakes.takeRandomNum(6);
        return Strings.format("{}{}{}", YEAR, DAYS, RANDOM);
    }
    public static BigDecimal getNextWeekFallPrice(BigDecimal maxPrice) {
        return maxPrice.multiply(BigDecimal.valueOf(0.0132)).setScale(0, RoundingMode.HALF_UP);
    }

    /*
     * 功能描述: 按照首字母分类排序
     * 〈〉
     *
     * @param null 1
     * @return :
     */
    public static List<Map<String,Object>> brandInitialGroup(List<Map> list){
        Map<String,List<Map<String,Object>>> m = new HashMap<>();
        List<Map<String,Object>> lists= new ArrayList<Map<String,Object>>();
        for(Map<String, Object> data :list){
            data.put("initial",data.get("initial"));
        }
        Collections.sort(list , (o1, o2) -> {
            String o1Value = String.valueOf(o1.get("initial").toString());
            String o2Value = String.valueOf(o2.get("initial").toString());
            return o1Value.compareTo(o2Value);
        });
        for (Map<String, Object> data :list){
            if(m.get(data.get("initial").toString())==null){
                List<Map<String,Object>> a = new ArrayList<>();
                a.add(data);
                m.put(data.get("initial").toString(), a);
            }else{
                List<Map<String,Object>> a = m.get(data.get("initial").toString());
                a.add(data);
                m.put(data.get("initial").toString(), a);
            }
        }
        for (String key : m.keySet()) {
            Map<String, Object> map = new HashMap<>();
            map.put("initial",key);
            map.put("list",m.get(key));
            lists.add(map);
        }
        return lists;
    }

    public static BigDecimal formatEvaluate(BigDecimal evaluatePrice) {
        try {
            if (Objects.isNull(evaluatePrice) || evaluatePrice.intValue() <= 0) {
                return BigDecimal.ZERO;
            }
            if (evaluatePrice.intValue() < 100) {
                return evaluatePrice;
            }
            final char[] charArray = String.valueOf(evaluatePrice.intValue()).toCharArray();
            int sum = 0;
            String numString = "";
            for (int i = 0; i < charArray.length - 1; i++) {
                final int numericValue = Character.getNumericValue(charArray[i]);
                sum += numericValue;
                numString = numString.concat(String.valueOf(numericValue));
            }
            int remainder = sum % 10;
            return  new BigDecimal(numString.concat(String.valueOf(remainder)));
        } catch (Exception e) {
            return evaluatePrice;
        }
    }
}
